import svelte from 'rollup-plugin-svelte';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
import serve from 'rollup-plugin-serve';
import babel from 'rollup-plugin-babel';
import preprocess from 'svelte-preprocess';
import css from 'rollup-plugin-css-only';
import json from "@rollup/plugin-json";
import { uglify } from 'rollup-plugin-uglify'

const production = !process.env.ROLLUP_WATCH;

export default {
	input: 'src/main.js',
    /**
     *  amd: 异步模块定义，用于像RequestJS这样的模块加载器。
     *  cjs: CommonJS,适用于Node或Browserify/webpack。
     *  es: 将软件包保存为ES模块文件。
     *  iife: 一个自动执行的功能，适合作为script标签这样的，只能在浏览器中运行。
     *  umd: 通用模块定义，以amd、cjs和iife为一体。
     */
	output: {
		sourcemap: false,
		name: 'app',
        format: 'umd',
        file: 'public/build/bundle.js'
	},
	plugins: [
		svelte({
			compilerOptions: {
				// enable run-time checks when not in production
				dev: !production
			},
            preprocess: preprocess()
		}),

        babel({ // 执行 babel 时自动去 .babelrc 文件加载对应配置
            exclude: 'node_modules/**'  // 排除
        }),

        // we'll extract any component CSS out into
		// a separate file - better for performance
		css({ output: 'bundle.css' }),

        json(),

		// If you have external dependencies installed from
		// npm, you'll most likely need these plugins. In
		// some cases you'll need additional configuration -
		// consult the documentation for details:
		// https://github.com/rollup/plugins/tree/master/packages/commonjs
		resolve({
			browser: true,
			dedupe: ['svelte']
		}),
		commonjs(),

		// In dev mode, call `npm run start` once
		// the bundle has been generated
		!production && serve({
            open: false,
            openPage: '/public/index.html',
            port: 3000,
            contentBase: ''
        }),

		// Watch the `public` directory and refresh the
		// browser on changes when not in production
		!production && livereload('public'),

		// If we're building for production (npm run build
		// instead of npm run dev), minify
		production && terser(),

        production && uglify(), // 压缩代码的插件
	],
	watch: {
		clearScreen: false
	}
};
